﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Maticsoft.Model.NewsManage;
using Maticsoft.BLL.NewsManage;
using Maticsoft.BLL;
using System.Text;
using System.Globalization;
namespace Maticsoft.Web
{
    public class CommonPage : System.Web.UI.Page
    {

        

        private const int topCount = 200;

        Maticsoft.BLL.Sseo.S_Seo bll = new Maticsoft.BLL.Sseo.S_Seo();

        Maticsoft.BLL.NewsManage.NewsClass dalNewsClass = new Maticsoft.BLL.NewsManage.NewsClass();

        Maticsoft.BLL.NewsManage.News dalNews = new Maticsoft.BLL.NewsManage.News();

        public NewsDto commonNewsDto = null;


        public Maticsoft.Model.Sseo.S_Seo commonMod;

        private  const  int id = 1;

        public string nvaTitle = "";

        public CommonPage()
        {
            InitializeComponent();
        }
        public void InitializeComponent()
        {
            commonMod = bll.GetModel(id);
           

        }
        protected string FormatString(string str,int length)
        {
            if (str.Length > length)
            {
                str = str.Substring(0, length-1) + "...";
            }
            return str;
        }

        protected string FormatJobString(string str)
        {
            if (str == null || str.Equals(""))
            {
                str = "--";
            }
            return str;
        }

        protected string FormatJobDescString(string str)
        {
            if (str == null || str.Equals(""))
            {
                str = "无描述信息";
            }
            return str;
        }

        protected string GetJobDeployDate(string str)
        {
            string rsult="";
            DateTime currentDate = DateTime.Now;
            if (str == null || str.Equals(""))
            {
                rsult = "0秒前";
            }
            else {

                DateTime oldDate = DateTime.Parse(str);
                System.TimeSpan st = currentDate.Subtract(oldDate);
                rsult = st.Days.ToString() + "天"
                 + st.Hours.ToString() + "小时"
                 + st.Minutes.ToString() + "分钟"
                 + st.Seconds.ToString() + "秒"; 
            }
            return rsult;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
         
            int newId = 0;
            DataSet newClassDataSet = null;
            Maticsoft.Model.NewsManage.NewsClass newClass = null;
            if (!IsPostBack)
            {
                //1.get parames news id
                if ((Request.Params[PubConstant.PARM_NEWSID] != null) && (Request.Params[PubConstant.PARM_NEWSID].ToString() != ""))
                {
                    newId = int.Parse(Request.Params[PubConstant.PARM_NEWSID]);
                   
                }
                //if have new id ,create commonNewsDto
                if (newId > 0)
                {
                    commonNewsDto = getNavDto(newId);
                }
                //if commonNewsDto is null,
                if (commonNewsDto==null)
                {
                    commonNewsDto = new NewsDto();

                    int menuId = 0;
                    int navMenuOne = 0;
                    int navMenuTwo = 0;
                    if ((Request.Params[PubConstant.MENU_PARM] != null) && (Request.Params[PubConstant.MENU_PARM].ToString() != ""))
                    {
                        menuId = int.Parse(Request.Params[PubConstant.MENU_PARM]);
                    }
                    if ((Request.Params[PubConstant.NAV_PARM_ONE] != null) && (Request.Params[PubConstant.NAV_PARM_ONE].ToString() != ""))
                    {
                        navMenuOne = int.Parse(Request.Params[PubConstant.NAV_PARM_ONE]);
                    }
                    if ((Request.Params[PubConstant.NAV_PARM_TWO] != null) && (Request.Params[PubConstant.NAV_PARM_TWO].ToString() != ""))
                    {
                        navMenuTwo = int.Parse(Request.Params[PubConstant.NAV_PARM_TWO]);
                    }
                  
                    PubConstant.MENU_INDEX menuIndex = PubConstant.MENU_INDEX.首页;
                    try
                    {
                        menuIndex = (PubConstant.MENU_INDEX)Enum.ToObject(typeof(PubConstant.MENU_INDEX), menuId);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                    
                    commonNewsDto.menuIndex = menuIndex;
                    if (navMenuTwo > 0)
                    {
                      commonNewsDto.menu_grade=PubConstant.MENU_GRADE.三级菜单;
                      newClass = dalNewsClass.GetModel(navMenuTwo);
                      commonNewsDto.firstNewsClass = dalNewsClass.GetModel(navMenuOne);
                      commonNewsDto.secondNewsClass = newClass;
                    }else{
                      if(navMenuOne>0){
                         commonNewsDto.menu_grade=PubConstant.MENU_GRADE.二级菜单;
                         newClass = dalNewsClass.GetModel(navMenuOne);
                         commonNewsDto.firstNewsClass = newClass;
                         commonNewsDto.secondNewsClass = null;
                     }
                     else{
                          commonNewsDto.menu_grade = PubConstant.MENU_GRADE.一级菜单;
                          newClassDataSet = dalNewsClass.getListByClassDesc(commonNewsDto.menuIndex.ToString());
                          commonNewsDto.firstNewsClass = null;
                          commonNewsDto.secondNewsClass = null;
                      }
                    }

                }

                //
                if (newClassDataSet != null)
                {
                    if (newClassDataSet.Tables[0].Rows.Count > 0)
                    {
                        newClass = new Maticsoft.Model.NewsManage.NewsClass();
                        newClass.ClassDesc = newClassDataSet.Tables[0].Rows[0]["ClassDesc"].ToString();
                        newClass.ClassId = int.Parse(newClassDataSet.Tables[0].Rows[0]["ClassId"].ToString());
                        newClass.ifEifOpen = Convert.ToBoolean(newClassDataSet.Tables[0].Rows[0]["ifEifOpen"]);
                    }
                }
                //
                if (newClass==null)
                {
                    newClass = new Maticsoft.Model.NewsManage.NewsClass();
                    newClass.ifEifOpen = false;
                }
                //
                if (commonNewsDto.news == null && newId > 0 )
                {
                    commonNewsDto.news = dalNews.GetModel(newId);
                }

                //
                switch (commonNewsDto.menu_grade)
                {
                    case PubConstant.MENU_GRADE.二级菜单:
                        {
                            if (newClass.ifEifOpen)
                            {
                                commonNewsDto.newsList = dalNews.GetNewsList(topCount, commonNewsDto.firstNewsClass.ClassId, false);
                                commonNewsDto.news = createModel(commonNewsDto.newsList);
                                commonNewsDto.news_type = PubConstant.NEWS_TYPE.列表新闻;
                            }
                            else
                            {
                                commonNewsDto.newsList = null;
                                commonNewsDto.news_type = PubConstant.NEWS_TYPE.单条新闻;
                                if (commonNewsDto.news == null || newId <= 0)
                                {
                                    DataSet tempDataSet = dalNews.GetNewsList(topCount, commonNewsDto.firstNewsClass.ClassId, false);
                                     commonNewsDto.news = createModel(tempDataSet);
                                     
                                }
                            }
                            break;
                        }
                    case PubConstant.MENU_GRADE.三级菜单:
                        {

                            if (newClass.ifEifOpen)
                            {
                                commonNewsDto.news_type = PubConstant.NEWS_TYPE.列表新闻;
                                commonNewsDto.newsList = dalNews.GetNewsList(topCount, commonNewsDto.secondNewsClass.ClassId, false);
                                commonNewsDto.news = createModel(commonNewsDto.newsList);

                            }
                            else
                            {
                                commonNewsDto.news_type = PubConstant.NEWS_TYPE.单条新闻;
                                commonNewsDto.newsList = null;
                                if (commonNewsDto.news == null || newId <= 0)
                                {
                                    DataSet tempDataSet = dalNews.GetNewsList(topCount, commonNewsDto.secondNewsClass.ClassId, false);
                                    commonNewsDto.news = createModel(tempDataSet);

                                }
                            }

                            break;
                        }

                    default:
                        {
                            if (newClass.ifEifOpen)
                            {
                                commonNewsDto.news_type = PubConstant.NEWS_TYPE.列表新闻;
                                commonNewsDto.newsList = dalNews.GetNewsList(topCount, newClass.ClassId, false);
                                commonNewsDto.news = createModel(commonNewsDto.newsList);
                            }
                            else
                            {
                                commonNewsDto.news_type = PubConstant.NEWS_TYPE.单条新闻;
                                commonNewsDto.newsList = null;
                                if (commonNewsDto.news == null || newId <= 0)
                                {
                                    DataSet tempDataSet = dalNews.GetNewsList(topCount, newClass.ClassId, false);
                                    commonNewsDto.news = createModel(tempDataSet);

                                }
                            }
                            break;
                        }
                }
            }

        }

        private Maticsoft.Model.NewsManage.News createModel(DataSet ds)
        {
            Maticsoft.Model.NewsManage.News model = new Maticsoft.Model.NewsManage.News();
            #region 字段信息
            if (ds != null)
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                    {
                        model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                    }
                    model.Heading = ds.Tables[0].Rows[0]["Heading"].ToString();
                    model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                    if (ds.Tables[0].Rows[0]["Frequency"].ToString() != "")
                    {
                        model.Frequency = int.Parse(ds.Tables[0].Rows[0]["Frequency"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["IssueDate"].ToString() != "")
                    {
                        model.IssueDate = DateTime.Parse(ds.Tables[0].Rows[0]["IssueDate"].ToString());
                    }
                    model.Dormancy = ds.Tables[0].Rows[0]["Dormancy"].ToString();
                    if (ds.Tables[0].Rows[0]["ClassId"].ToString() != "")
                    {
                        model.ClassId = int.Parse(ds.Tables[0].Rows[0]["ClassId"].ToString());
                    }
                    model.Focus = ds.Tables[0].Rows[0]["Focus"].ToString();
                    if (ds.Tables[0].Rows[0]["Priority"].ToString() != "")
                    {
                        model.Priority = int.Parse(ds.Tables[0].Rows[0]["Priority"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["IsTop"].ToString() != "")
                    {
                        model.IsTop = int.Parse(ds.Tables[0].Rows[0]["IsTop"].ToString());
                    }
                    if (ds.Tables[0].Rows[0]["isRead"].ToString() != "")
                    {
                        model.isRead = Convert.ToBoolean(ds.Tables[0].Rows[0]["isRead"]);
                    }
                    model.IsIamge = ds.Tables[0].Rows[0]["IsIamge"].ToString();
                    model.Image = ds.Tables[0].Rows[0]["Image"].ToString();
                }
            }
            #endregion
            return model;
        }

        public NewsDto getNavDto(int newId)
        {
            NewsDto newsDto = new NewsDto();
            Maticsoft.Model.NewsManage.NewsClass newsClassObjFirst = null;
            Maticsoft.Model.NewsManage.NewsClass newsClassObjSecond = null;
            Maticsoft.Model.NewsManage.NewsClass newsClassObjThread = null;
            Maticsoft.Model.NewsManage.NewsClass newsClassObjFourth = new Maticsoft.Model.NewsManage.NewsClass();
            newsClassObjFourth.ClassDesc="";
            newsClassObjFourth.ClassId = 0;
            Maticsoft.Model.NewsManage.News news = dalNews.GetModel(newId);
            if (news != null)
            {
                newsClassObjFirst = dalNewsClass.GetModel(news.ClassId);
                if (newsClassObjFirst != null && (newsClassObjFirst.ParentId != null) && newsClassObjFirst.ParentId != 0)
                {
                    newsClassObjSecond = dalNewsClass.GetModel(newsClassObjFirst.ParentId);
                    if (newsClassObjSecond != null && (newsClassObjSecond.ParentId != null) && newsClassObjSecond.ParentId != 0)
                    {
                        newsClassObjThread = dalNewsClass.GetModel(newsClassObjSecond.ParentId);
                        if (newsClassObjThread != null && (newsClassObjThread.ParentId != null) && newsClassObjThread.ParentId == 0)
                        {

                            PubConstant.MENU_INDEX menuIndexThread = (PubConstant.MENU_INDEX)Enum.Parse(typeof(PubConstant.MENU_INDEX), newsClassObjThread.ClassDesc, false);
                            newsDto.menuIndex = menuIndexThread;
                            newsDto.menu_grade = PubConstant.MENU_GRADE.三级菜单;
                            newsDto.firstNewsClass = newsClassObjSecond;
                            newsDto.secondNewsClass = newsClassObjFirst;
                        }
                        else
                        {
                            newsDto.menuIndex = PubConstant.MENU_INDEX.首页;
                            newsDto.menu_grade = PubConstant.MENU_GRADE.一级菜单;
                            newsDto.firstNewsClass = newsClassObjFourth;
                            newsDto.secondNewsClass = newsClassObjFourth;

                        }


                    }
                    else
                    {
                        if (newsClassObjSecond != null && (newsClassObjSecond.ParentId != null) && newsClassObjSecond.ParentId == 0)
                        {

                            PubConstant.MENU_INDEX menuIndexSecond = (PubConstant.MENU_INDEX)Enum.Parse(typeof(PubConstant.MENU_INDEX), newsClassObjSecond.ClassDesc, false);
                            newsDto.menuIndex = menuIndexSecond;
                            newsDto.menu_grade = PubConstant.MENU_GRADE.二级菜单;
                            newsDto.firstNewsClass = newsClassObjFirst;
                            newsDto.secondNewsClass = newsClassObjFourth;
                        }
                        else
                        {
                            newsDto.menuIndex = PubConstant.MENU_INDEX.首页;
                            newsDto.menu_grade = PubConstant.MENU_GRADE.一级菜单;
                            newsDto.firstNewsClass = newsClassObjFourth;
                            newsDto.secondNewsClass = newsClassObjFourth;

                        }

                    }
                }
                else
                {
                    if (newsClassObjSecond != null && (newsClassObjSecond.ParentId != null) && newsClassObjSecond.ParentId == 0)
                    {
                        newsDto.menuIndex = PubConstant.MENU_INDEX.首页;
                        newsDto.menu_grade = PubConstant.MENU_GRADE.一级菜单;
                        newsDto.firstNewsClass = newsClassObjFourth;
                        newsDto.secondNewsClass = newsClassObjFourth;
                    }
                    else
                    {
                        newsDto.menuIndex = PubConstant.MENU_INDEX.首页;
                        newsDto.menu_grade = PubConstant.MENU_GRADE.一级菜单;
                        newsDto.firstNewsClass = newsClassObjFourth;
                        newsDto.secondNewsClass = newsClassObjFourth;

                    }


                }

            }
            else
            {

                newsDto.menuIndex = PubConstant.MENU_INDEX.首页;
                newsDto.menu_grade = PubConstant.MENU_GRADE.一级菜单;
                newsDto.firstNewsClass = newsClassObjFourth;
                newsDto.secondNewsClass = newsClassObjFourth;
            }

            return newsDto;
        }

        public string generateHref(string action, PubConstant.MENU_INDEX enumMenu, int navMenuOne, int navMenuTwo)
        {
            StringBuilder strHref = new StringBuilder();
            string hrefText = PubConstant.getMenuPage()[enumMenu];
            if (action != "")
            {
                hrefText = action;

            }
            if (hrefText != null && hrefText != "")
            {
                strHref.Append(hrefText);
                strHref.Append("?");
                strHref.Append(PubConstant.MENU_PARM);
                strHref.Append("=");
                strHref.Append((int)enumMenu);
                strHref.Append("&");
                strHref.Append(PubConstant.NAV_PARM_ONE);
                strHref.Append("=");
                strHref.Append(navMenuOne);
                strHref.Append("&");
                strHref.Append(PubConstant.NAV_PARM_TWO);
                strHref.Append("=");
                strHref.Append(navMenuTwo);

            }
            return strHref.ToString();
        }

        public string createSplite() { 
            StringBuilder strHref = new StringBuilder();
            strHref.Append("<img src='../Images/io.gif'/>");
            return strHref.ToString();
        }

        public DataTable getPagedTable(DataTable dt, int innerPageIndex, int innerPageSize)
        {

            if (innerPageIndex == 0)
                return dt;
            DataTable newdt = dt.Clone();
            //newdt.Clear();
            int rowbegin = (innerPageIndex - 1) * innerPageSize;
            int rowend = innerPageIndex * innerPageSize;

            if (rowbegin >= dt.Rows.Count)
                return newdt;

            if (rowend > dt.Rows.Count)
                rowend = dt.Rows.Count;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                DataRow newdr = newdt.NewRow();
                DataRow dr = dt.Rows[i];
                foreach (DataColumn column in dt.Columns)
                {
                    newdr[column.ColumnName] = dr[column.ColumnName];
                }
                newdt.Rows.Add(newdr);
            }

            return newdt;
        }
    }
}
